package org.zxp.designpattern.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: effectiveJava
 * @description:
 * @author: X-Pacific zhang
 * @create: 2019-08-24 13:34
 **/
public class ServiceFactory {
    private static Map<ServiceType, Service> map = new HashMap<>();

    public synchronized static Service getService(ServiceType serviceType) {
        if (map.containsKey(serviceType)) {
            return map.get(serviceType);
        }
        Service service = null;
        switch (serviceType) {
            case ONE:
                service = new Service1();
            case TWO:
                service = new Service2();
            case THREE:
                service = new Service3();
        }
        if(service != null)
            map.put(serviceType,service);
        return service;
    }
    static enum ServiceType {
        ONE, TWO, THREE
    }
}
